import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import cn.thu.thss.mla.logic.LogAnalyser;

public class AnalyseMain {
	private static LogAnalyser logAnalyser;
	private static PrintWriter goodResultWriter;
	private static PrintWriter badResultWriter;
	public static void main(String[] args) throws IOException {
		File logDir = new File("logs");
		if (logDir.isDirectory()) {
			File[] subDirs = logDir.listFiles();
			for (File file : subDirs) {
				if (file.isDirectory()) {
					File[] logFiles = file.listFiles();
					String fileName = "result" + file.getName();
					logAnalyser = new LogAnalyser();
					goodResultWriter = new PrintWriter(new FileWriter(fileName + ".good"));
					badResultWriter = new PrintWriter(new FileWriter(fileName + ".bad"));
					for (File logFile : logFiles) {
						System.out.println("Scan file: " + logFile.getName());
						BufferedReader in = new BufferedReader(new FileReader(logFile));
						String line = in.readLine();
						while (line != null && !line.isEmpty()) {
							logAnalyser.processLogLine(line);
							line = in.readLine();
						}
						in.close();
					}
					logAnalyser.printResults(goodResultWriter, badResultWriter);
					goodResultWriter.close();
					badResultWriter.close();
				}
			}
		}
	}
}